package com.atomic.test.example.controller;

import com.atomic.test.example.common.Result;
import com.atomic.test.example.entity.Board;
import com.atomic.test.example.service.IBoardService;
import com.atomic.test.example.utils.CheckDataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import static com.atomic.test.example.common.ErrorCode.PARAMETER_ERROR;
import static com.atomic.test.example.common.ErrorCode.STATUS_ERROR;
import static com.atomic.test.example.utils.CheckDataUtil.updateBoardCheck;


/**
 * Created by dreamyao on 2017/1/27.
 */
@Controller
@RequestMapping(value = "/board")
public class BoardController {

    @Autowired
    private IBoardService boardService;

    /**
     * 创建论坛板块
     *
     * @param board
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/createBoard", method = RequestMethod.POST)
    public Result<Integer> createBoard(@RequestBody Board board) {
        if (!CheckDataUtil.createBoardCheck(board)) {
            return new Result<>(STATUS_ERROR, "参数错误!");
        }
        Integer boardNum = boardService.createBoard(board);
        return new Result<>(boardNum);
    }

    /**
     * 根据论坛板块名称查询论坛信息
     *
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getBoardMessage", method = RequestMethod.POST)
    public Result<Board> getBoardMessage(String boardName) {
        if (boardName.equals("") && boardName.isEmpty()) {
            return new Result<>(PARAMETER_ERROR, "参数错误!");
        }
        Board board = boardService.getBoardMassage(boardName);

        return new Result<>(board);
    }

    /**
     * 论坛信息更新
     *
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result<Boolean> updateBoard(@RequestBody Board board) {
        Result<Boolean> result = null;
        if (!updateBoardCheck(board)) {
            result.setCode(PARAMETER_ERROR);
            result.setMessage("参数错误!");
            return result;
        }
        boardService.update(board);
        return new Result<>(true);
    }

    /**
     * 根据论坛板块Id删除论坛板块信息
     *
     * @param boardId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteBoard", method = RequestMethod.POST)
    public Result<Boolean> deleteBoard(Integer boardId) {
        if (boardId == null || boardId <= 0) {
            return new Result<>(PARAMETER_ERROR, "参数错误!");
        }
        boardService.deleteBoard(boardId);
        return new Result<>(true);
    }

    /**
     * 获取所有论坛板块信息
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAllBoard", method = RequestMethod.GET)
    public Result<List<Board>> getAllBoard() {
        List<Board> boards = boardService.findAllBoard();
        return new Result<>(boards);
    }
}
